package myservlet.control;

import java.io.IOException;
import java.io.PrintWriter;
import java.sql.Connection;
import java.sql.DriverManager;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

import javax.servlet.ServletConfig;
import javax.servlet.ServletException;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import mybean.data.login;

/*管理员--修改单车信息*/
public class changeBike extends HttpServlet{
	public void init(ServletConfig config) throws ServletException {
		// TODO Auto-generated method stub
			super.init(config);
			try {//加载驱动
				Class.forName("com.mysql.jdbc.Driver");	
			}
			catch(Exception e) {}
		}
	public  void doPost(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		req.setCharacterEncoding("utf-8");
		resp.setContentType("text/html;charset=UTF-8");
		PrintWriter out = resp.getWriter();
		//获取用户更改后的参数  
		String bid = req.getParameter("bid"),//参数名要与input里的name值对应
			distract= req.getParameter("distract"),//地区名
			memo= req.getParameter("memo"),
			manager= req.getParameter("manager"),
			price= req.getParameter("price"),
			deposit= req.getParameter("deposit");
		if(distract=="" || memo=="" || manager=="" || price=="" ||deposit=="") {
			out.println("<script language = javascript>alert('请把信息填写完整!');");
			out.println("location.href='lookBikes.jsp'</script>"); 
			return;
		}
		login loginBean =null; 
		HttpSession session= req.getSession(true);
		Connection con = null;
		try {
			loginBean = (login)session.getAttribute("loginBean");
			boolean b=loginBean==null || loginBean.getLogname()==null || loginBean.getLogname().length()==0;
			if(b) {
				out.println("<script language = javascript>alert('请重新登录!');");
				out.println("location.href='index.jsp'</script>"); 
			}
			String url = "jdbc:mysql://127.0.0.1:3306/bike?&useSSL=false&serverTimezone=GMT%2B8&allowMultiQueries=true";
			PreparedStatement sql;
			ResultSet rs;
			//修改单车信息
			try {//连接数据库
				con = DriverManager.getConnection(url,"root","744615");
				//查询该地区对应的编号，该管理员对应的编号
				String dno="",mno="";
				String condition="select Dno from district where Dname='"+distract+"'";
				sql=con.prepareStatement(condition);
				con.setAutoCommit(false);
				rs = sql.executeQuery(condition);
				if(rs.next()==true)
					dno = rs.getString("Dno");
				else {
					out.println("<script language = javascript>alert('地区不存在，请重新输入!');");
					out.println("location.href='lookBikes.jsp'</script>"); 
					return;
				}
				sql.close();
				System.out.println(dno);
				condition="select MID from manager where Mname='"+manager+"'";
				sql=con.prepareStatement(condition);
				rs = sql.executeQuery(condition);
				if(rs.next()==true)
					mno = rs.getString("MID");
				else {
					out.println("<script language = javascript>alert('管理人不存在，请重新输入!');");
					out.println("location.href='lookBikes.jsp'</script>"); 
					return;
				}
				sql.close();
				System.out.println(mno);
				condition="update bike set B_district=?,B_memo=?,B_manager=?,price=?,B_deposit=? where B_id=?"; 
				sql=con.prepareStatement(condition);
				sql.setString(1, dno);  
				sql.setString(2, memo);
				sql.setString(3, mno);
				sql.setString(4, price);
				sql.setString(5, deposit);
				sql.setString(6, bid);
				sql.executeUpdate();  //执行更新操作，  更新数据库 
	            //sql.close(); 
				// 若成功执行完所有的更新操作，则正常结束 
				con.commit();//进行手动提交（commit） 
				con.setAutoCommit(true); //提交完成后回复现场将Auto commit,还原为true
				System.out.println(mno);
				System.out.println(deposit);
				out.println("<script language = javascript>alert('修改成功!');");
				out.println("location.href='lookBikes.jsp'</script>"); 
			}
			catch (SQLException exp){
				System.out.println(exp);
				out.println("<script language = javascript>alert('数据库关系失败，修改失败!');");
				out.println("location.href='lookBikes.jsp'</script>"); 
			}
			finally{   
	            // 关闭Connection  
	            con.close();   
			}
		}
		catch (Exception exp){
			System.out.println(exp);
			out.println("<script language = javascript>alert('修改失败!');");
			out.println("location.href='lookBikes.jsp'</script>"); 
		}
	}	
	public void doGet(HttpServletRequest req, HttpServletResponse resp) throws ServletException, IOException {
		// TODO Auto-generated method stub
		doPost(req, resp);
	}
}
